package com.panasonic.web.controller.cms;

import com.panasonic.common.core.controller.BaseController;
import com.panasonic.common.core.domain.AjaxResult;
import com.panasonic.common.core.page.TableDataInfo;
import com.panasonic.common.utils.ExcelUtil;
import com.panasonic.web.domain.system.SysOperLog;
import com.panasonic.web.service.ISysOperLogService;
import com.sun.mail.util.MailSSLSocketFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;


/**
 * 操作日志记录
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/monitor/operlog")
public class SysOperlogController extends BaseController
{
    @Autowired
    private ISysOperLogService operLogService;

    @GetMapping("/list")
    public TableDataInfo list(SysOperLog operLog)
    {
        startPage();
        List<SysOperLog> list = operLogService.selectOperLogList(operLog);
        return getDataTable(list);
    }

    @GetMapping("/export")
    public AjaxResult export(SysOperLog operLog)
    {
        List<SysOperLog> list = operLogService.selectOperLogList(operLog);
        ExcelUtil<SysOperLog> util = new ExcelUtil<SysOperLog>(SysOperLog.class);
        return util.exportExcel(list, "操作日志");
    }

    @DeleteMapping("/{operIds}")
    public AjaxResult remove(@PathVariable Long[] operIds)
    {
        return toAjax(operLogService.deleteOperLogByIds(operIds));
    }

    @DeleteMapping("/clean")
    public AjaxResult clean()
    {
        operLogService.cleanOperLog();
        return AjaxResult.success();
    }

    @PostMapping("/send")
    public AjaxResult sendEmail(@RequestBody EmailProps config) throws GeneralSecurityException, MessagingException {

        Properties props = new Properties();

        // 开启debug调试
        props.put("mail.debug", "true");
        props.put("mail.transport.protocol", "smtp");
        // 发送服务器需要身份验证
        props.put("mail.smtp.auth", config.getAuth());
        props.put("mail.host", config.getHost());
        props.put("mail.smtp.port", config.getPort());

        if(config.getStarttls()!=null){
            props.put("mail.smtp.starttls.enable", config.getStarttls());
        }
        if(config.getRequired()!=null){
            props.put("mail.smtp.starttls.required", config.getRequired());
        }
        if(config.getSslenable()!=null){
            props.put("mail.smtp.ssl.enable", config.getSslenable());
        }

        if(config.getSocketFactoryClass()!=null){
            props.put("mail.smtp.socketFactory.class", config.getSocketFactoryClass());
        }

        if(config.getSocketFactoryPort()!=null){
            props.put("mail.smtp.socketFactory.port", config.getSocketFactoryPort());
        }
        if(config.getFallback()!=null){
            props.put("mail.smtp.socketFactory.fallback", config.getFallback());
        }
        Session session = Session.getInstance(props);
        Message msg = new MimeMessage(session);
        msg.setSubject("seenews 错误");
        msg.setText("村上春树");
        msg.setFrom(new InternetAddress(config.getUsername()));

        Transport transport = session.getTransport();
        transport.connect(config.getHost(), config.getUsername(), config.getPassword());
        transport.sendMessage(msg, new Address[] { new InternetAddress("245304687@qq.com") });
        transport.close();
        return AjaxResult.success();
    }


}
